home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: 'Freeing' storage with realloc
- Date: Sat, 17 Feb 96 22:27:06 GMT
- Organization: none
- Distribution: world
- Message-ID: <824596026snz@genesis.demon.co.uk>
- References: <4fv44f$p2v@news.rz.uni-passau.de>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4fv44f$p2v@news.rz.uni-passau.de>
- berndl@sidonius.uni-passau.de "Klaus Berndl" writes:
-
- >
- >Please look at the snipped of code:
- >
- >#include <stdio.h>
- >
- >main () {
- >
- > char* buf = NULL;
- >
- > buf = (char*)malloc(100);
- > strcpy(buf, "Klaus Berndl");
- > printf("\n%s\n", buf);
- >
- >/* line 10 */ buf = (char*)realloc(buf, strlen(buf)+1); /* line 10 */
- >
- > if (buf)
- > printf("\n%s\n", buf);
- > else
- > printf("\nshit!\n");
- >}
- >
- >My questions are now: How much memory is allocated for 'buf' after
- >executing line 10?! Does realloc 'freeing' the storage behind byte 13?
- >Or are still 100 bytes allocated for buf?
-
- The extra storage is in principle if not in practice freed. All that is
- important as far as the program is concerned is that it is illegal to
- access buf[13] or beyond after the realloc. Whether a particular
- implementation leaves the storage in place is up to the implementation
- although any reasonable implementation will make the space available for
- further allocation where possible. If you still need to use the space as
- part of that array then that call to realloc is inappropriate and should
- be removed or changed to a more suitable value.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-